﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Demos.util;
using DiaoPaiDaYin;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.样品管理.样品订单预览;
using DXApplicationTangche.UC.订单.service;
using DXApplicationTangche.UC.订单一览新.订单预览新;
using DXApplicationTangche.UC.订单一览新.订单预览新.model;
using DXApplicationTangche.UC.销售台账.service;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.生产下单
{
    public partial class XtraUserControl坚山xml看板 : DevExpress.XtraEditors.XtraUserControl
    {
        private 销售排产Model model;
        public XtraUserControl坚山xml看板()
        {
            InitializeComponent();
            this.label提示.Text = "坚山xml订单看板";
        }

        private void dateTimePicker日期_CloseUp(object sender, EventArgs e)
        {
            this.model = new 销售排产Model(this.dateTimePicker日期.Value.ToString("yyyy-MM-dd"));
            this.gridControl坚山xml看板.DataSource = this.model.kashiyamaOrderStatusSaleIndexFacadesVO;
            this.advBandedGridView坚山xml看板.RefreshData();
        }

        private void simpleButton确认_Click(object sender, EventArgs e)
        {
            if (this.model.kashiyamaOrderStatusSaleIndexFacadesVO.Count > 0)
            {
                //  筛选的待排产订单:订单状态为未排产
                List<销售日记账预览VO> 销售日记账预览VOs =
                    this.model.build坚山xml确认前预览().销售日记账预览VO;
                if (销售日记账预览VOs.Count > 0)
                {
                    new Form销售日记账确认预览(销售日记账预览VOs).ShowDialog();
                }
                else
                {
                    MessageBox.Show("无待排产订单");
                    return;
                }
            }
            else
            {
                MessageBox.Show("无待排产订单");
                return;
            }
            //if (this.model.kashiyamaOrderStatusSaleIndexFacadesVO.Count > 0)
            //{
            //    //  筛选未排产
            //    List<OrderDailyConfirmDTO> orderDailyConfirmDTOs =
            //        this.model.build坚山xml确认前筛选未排产().orderDailyConfirmDTOs;
            //    //  筛选结果判断
            //    if (orderDailyConfirmDTOs.Count <= 0)
            //    {
            //        MessageBox.Show("无待排产订单");
            //        return;
            //    }
            //    else
            //    {
            //        FunctionHelper.JsonSerialization(orderDailyConfirmDTOs);
            //        销售台账Service.post确认销售台账(orderDailyConfirmDTOs);
            //        MessageBox.Show("确认完成");
            //    }
            //}
            //else
            //{
            //    MessageBox.Show("无待排产订单");
            //    return;
            //}
        }

        private void bandedGridView坚山xml看板_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            new Form订单预览新(
                    FunctionHelper.GetAdvBandedgridViewValue(
                        this.advBandedGridView坚山xml看板,
                        this.advBandedGridView坚山xml看板.FocusedRowHandle,
                        "orderId"
                        )
                    ).ShowDialog();
        }

        private void 取消ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String orderId = this.advBandedGridView坚山xml看板.GetRowCellValue(
                    this.advBandedGridView坚山xml看板.FocusedRowHandle, "orderId").ToString();
                DTO返回值判断 dTO返回值判断 = 订单Service.get清单取消判断(orderId);
                if ("200".Equals(dTO返回值判断.errorCode))
                {
                    RestCall.PostHttp(URL.接口url + "/pmc/orderCancel", orderId);
                    MessageBox.Show("已改为取消订单");
                }
                else
                {
                    MessageBox.Show(dTO返回值判断.errorMessage);
                }
            }
            catch
            {
                MessageBox.Show("修改订单状态失败");
            }
        }

        private void simpleButton导出excel_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出抖音订单";
            fileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                this.advBandedGridView坚山xml看板.ExportToXlsx(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("文件已保存至" + fileDialog.FileName, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void 同步订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String orderId = this.advBandedGridView坚山xml看板.GetRowCellValue(
                    this.advBandedGridView坚山xml看板.FocusedRowHandle, "orderId").ToString();
                //  坚山异常接口，同步订单一览
                销售台账Service.syncOrderStatusIndex(orderId);
            }
            catch { }
        }
    }

    public class 销售排产Model
    {
        //坚山xml销售日记账
        public List<KashiyamaOrderStatusSaleIndexFacade页面VO> kashiyamaOrderStatusSaleIndexFacadesVO = new List<KashiyamaOrderStatusSaleIndexFacade页面VO>();
        //其他订单销售日记账
        public List<OrderStatusSaleIndexDTO> list = new List<OrderStatusSaleIndexDTO>();
        //确认排产参数
        public List<OrderDailyConfirmDTO> orderDailyConfirmDTOs = new List<OrderDailyConfirmDTO>();

        public List<销售日记账预览VO> 销售日记账预览VO = new List<销售日记账预览VO>();
        public 销售排产Model(String dateStr)
        {
            List<KashiyamaOrderStatusSaleIndexFacadeDTO> kashiyamaOrderStatusSaleIndexFacades =
                销售台账Service.getKALatestXmlOrderData(dateStr);
            foreach (KashiyamaOrderStatusSaleIndexFacadeDTO item in kashiyamaOrderStatusSaleIndexFacades)
            {
                if (!String.IsNullOrWhiteSpace(item.orderStatusSaleIndexDTO.styleSizeCd))
                {
                    try { item.orderStatusSaleIndexDTO.styleSizeCd = item.orderStatusSaleIndexDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
                this.kashiyamaOrderStatusSaleIndexFacadesVO.Add(
                    new KashiyamaOrderStatusSaleIndexFacade页面VO(item)
                    );
            }
        }
        public 销售排产Model(String dateStr, String shopId)
        {
            if ("49".Equals(shopId))
            {
                //  美国坚山销售日记账
                this.list = 销售台账Service.getUSKashiyamaOrderStatusIndexPageable(dateStr, shopId);
            }
            else
            {
                this.list = 销售台账Service.get筛选销售台账(dateStr, shopId);
            }
            foreach (OrderStatusSaleIndexDTO item in this.list)
            {
                if (!String.IsNullOrWhiteSpace(item.styleSizeCd))
                {
                    try { item.styleSizeCd = item.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                    if (item.orderStatus.Equals(Const订单状态.NOT_SCHEDULED))
                    {
                        item.orderStatus = "未排产";
                    }
                    else if (item.orderStatus.Equals(Const订单状态.SCHEDULED))
                    {
                        item.orderStatus = "已排产";
                    }
                    else if (item.orderStatus.Equals(Const订单状态.WIP))
                    {
                        item.orderStatus = "在制品";
                    }
                    else if (item.orderStatus.Equals(Const订单状态.DONE))
                    {
                        item.orderStatus = "完成";
                    }
                    else if (item.orderStatus.Equals(Const订单状态.CANCEL))
                    {
                        item.orderStatus = "取消";
                    }
                }
                else
                {
                    if (item.orderStatus.Equals(Const订单状态.NOT_SCHEDULED))
                    {
                        item.orderStatus = "未排产";
                    }
                    else if (item.orderStatus.Equals(Const订单状态.SCHEDULED))
                    {
                        item.orderStatus = "已排产";
                    }
                    else if (item.orderStatus.Equals(Const订单状态.WIP))
                    {
                        item.orderStatus = "在制品";
                    }
                    else if (item.orderStatus.Equals(Const订单状态.DONE))
                    {
                        item.orderStatus = "完成";
                    }
                    else if (item.orderStatus.Equals(Const订单状态.CANCEL))
                    {
                        item.orderStatus = "取消";
                    }
                }
            }
        }
        public 销售排产Model build坚山xml确认前筛选未排产()
        {
            //  筛选的待排产订单:订单状态为
            foreach (KashiyamaOrderStatusSaleIndexFacade页面VO item in this.kashiyamaOrderStatusSaleIndexFacadesVO)
            {
                if (Const订单状态.NOT_SCHEDULED.Equals(item.orderStatus))
                {
                    this.orderDailyConfirmDTOs.Add(new OrderDailyConfirmDTO(item));
                }
            }
            return this;
        }
        public 销售排产Model buildXml外其他订单确认前筛选未排产()
        {
            //  筛选的待排产订单:订单状态为
            foreach (OrderStatusSaleIndexDTO 销售台账DTO in this.list)
            {
                if (Const订单状态.NOT_SCHEDULED.Equals(销售台账DTO.orderStatus))
                {
                    this.orderDailyConfirmDTOs.Add(new OrderDailyConfirmDTO(销售台账DTO));
                }
            }
            return this;
        }
        public 销售排产Model build坚山xml确认前预览()
        {
            //  筛选的待排产订单:订单状态为
            foreach (KashiyamaOrderStatusSaleIndexFacade页面VO item in this.kashiyamaOrderStatusSaleIndexFacadesVO)
            {
                if (Const订单状态.NOT_SCHEDULED.Equals(item.orderStatus)
                        || "未排产".Equals(item.orderStatus)
                    )
                {
                    this.销售日记账预览VO.Add(new 销售日记账预览VO(item));
                }
            }
            return this;
        }
        public 销售排产Model buildXml外其他订单确认前预览()
        {
            //  筛选的待排产订单:订单状态为
            foreach (OrderStatusSaleIndexDTO 销售台账DTO in this.list)
            {
                if (Const订单状态.NOT_SCHEDULED.Equals(销售台账DTO.orderStatus)
                        || "未排产".Equals(销售台账DTO.orderStatus)
                    )
                {
                    this.销售日记账预览VO.Add(new 销售日记账预览VO(销售台账DTO));
                }
            }
            return this;
        }
        public 销售排产Model build客户套号()
        {
            List<OrderStatusSaleIndexDTO> newList = new List<OrderStatusSaleIndexDTO>();
            foreach (OrderStatusSaleIndexDTO item in this.list)
            {
                if (!"凯丹店".Equals(item.customerName)
                        && !"春衫定制".Equals(item.customerName)
                        )
                {
                    newList.Add(item);
                }
            }
            this.list = newList;
            return this;
        }
        public 销售排产Model build补货套号()
        {
            List<OrderStatusSaleIndexDTO> newList = new List<OrderStatusSaleIndexDTO>();
            foreach (OrderStatusSaleIndexDTO item in this.list)
            {
                if ("凯丹店".Equals(item.customerName)
                        || "春衫定制".Equals(item.customerName)
                        )
                {
                    newList.Add(item);
                }
            }
            this.list = newList;
            return this;
        }

    }

    public class KashiyamaOrderStatusSaleIndexFacade页面VO
    {
        /**
         * KashiyamaJyutyuDTO的数据
         */
        public String id { get; set; }
        public String productNo { get; set; }   //  坚山xml数据的制造番号
        public String oyaProductNo { get; set; }    //  制造母番号
        public String seisanSosikiCd { get; set; }  //  组织编码
        public String item { get; set; }    //  项目
        public String brand { get; set; }   //  品牌
        public String period { get; set; }  //  季别
        public String itemNumber { get; set; }  //  品番
        public DateTime xmlOrderDate { get; set; }   //  下单日
        public DateTime xmlInputDt { get; set; } //  订单系统实际获取订单的日期（※截至21:00以后的订单都归到次日）
        public DateTime xmlKojyoShukaDt { get; set; }    // 出厂日
        public DateTime xmlDeliDateArrival { get; set; }     //  交付日
        public String custKana { get; set; }    //  客户姓名
        public String kijiNo { get; set; }  //  面料号
        public String kijiColor { get; set; }   //  面料颜色
        public String itemType { get; set; }    //  袖长:用于判断长袖还是短袖
        public String companyCd { get; set; }   //  公司编码
        public String productYear { get; set; }     //  生产年份
        public String kijiBrand { get; set; }   //  面料品牌
        public String txPeriod { get; set; }    //  面料年季
        public String szCd { get; set; }   //  尺寸
        public String katabanPtnCd { get; set; }    //  款式
        public DateTime createDate { get; set; }
        /**
         * 订单索引DTO-销售日记账DTO 
         */
        public String orderId { get; set; }
        //客户
        public String consignee { get; set; }  //  收件人
        public String customerId { get; set; }
        public String customerName { get; set; }
        public String mobile { get; set; }
        public String addressId { get; set; }
        public String addressProvince { get; set; }//省
        public String addressCity { get; set; }//市
        public String addressDistrict { get; set; }//区
        public String addressDetail { get; set; }//详细地址
        public String zipCode { get; set; }//邮编
        //面料
        public String materialNameCn { get; set; }//面料描述名称
        public String materialCode { get; set; }  //面料号
        public String composition { get; set; }//面料成分
        //款式
        public String styleSizeCd { get; set; }//尺码
        public String styleNameCn { get; set; }//款式名称
        public String refStyleId { get; set; }  //  标准款id
        //订单
        public String orderNo { get; set; } //  订单号
        public Double quantity { get; set; }    //  订单数量
        public DateTime orderDate { get; set; }//订单时间
        //订单状态
        public String orderStatus { get; set; }
        //店铺
        public String shopId { get; set; }
        public String shopName { get; set; }
        //销售确认
        public Boolean urgent { get; set; } //  是否加急
        public Boolean toBeConfirm { get; set; }  //  待确认
        public Boolean garmentModify { get; set; }//  成衣修改
        public String garmentBarcode { get; set; }//  成衣条码
        //价格
        public Double price { get; set; }//单价
        public Double totalPrice { get; set; }//总价

        public KashiyamaOrderStatusSaleIndexFacade页面VO(
            KashiyamaOrderStatusSaleIndexFacadeDTO dto
            )
        {
            this.id = dto.kashiyamaJyutyuDTO.id;
            this.productNo = dto.kashiyamaJyutyuDTO.productNo;   //  坚山xml数据的制造番号
            this.oyaProductNo = dto.kashiyamaJyutyuDTO.oyaProductNo;    //  制造母番号
            this.seisanSosikiCd = dto.kashiyamaJyutyuDTO.seisanSosikiCd;  //  组织编码
            this.item = dto.kashiyamaJyutyuDTO.item;    //  项目
            this.brand = dto.kashiyamaJyutyuDTO.brand;   //  品牌
            this.period = dto.kashiyamaJyutyuDTO.period;  //  季别
            this.itemNumber = dto.kashiyamaJyutyuDTO.itemNumber;  //  品番
            this.xmlOrderDate = dto.kashiyamaJyutyuDTO.orderDate;   //  下单日
            this.xmlInputDt = dto.kashiyamaJyutyuDTO.inputDt; //  订单系统实际获取订单的日期（※截至21:00以后的订单都归到次日）
            this.xmlKojyoShukaDt = dto.kashiyamaJyutyuDTO.kojyoShukaDt;    // 出厂日
            this.xmlDeliDateArrival = dto.kashiyamaJyutyuDTO.deliDateArrival;     //  交付日
            this.custKana = dto.kashiyamaJyutyuDTO.custKana;    //  客户姓名
            this.kijiNo = dto.kashiyamaJyutyuDTO.kijiNo;  //  面料号
            this.kijiColor = dto.kashiyamaJyutyuDTO.kijiColor;   //  面料颜色
            this.itemType = dto.kashiyamaJyutyuDTO.itemType;    //  袖长:用于判断长袖还是短袖
            this.companyCd = dto.kashiyamaJyutyuDTO.companyCd;   //  公司编码
            this.productYear = dto.kashiyamaJyutyuDTO.productYear;     //  生产年份
            this.kijiBrand = dto.kashiyamaJyutyuDTO.kijiBrand;   //  面料品牌
            this.txPeriod = dto.kashiyamaJyutyuDTO.txPeriod;    //  面料年季
            this.szCd = dto.kashiyamaJyutyuDTO.szCd;//尺寸
            this.katabanPtnCd = dto.kashiyamaJyutyuDTO.katabanPtnCd;//款式
            this.createDate = dto.kashiyamaJyutyuDTO.createDate;
            this.orderId = dto.orderStatusSaleIndexDTO.orderId;
            this.consignee = dto.orderStatusSaleIndexDTO.consignee;  //  收件人
            this.customerId = dto.orderStatusSaleIndexDTO.customerId;
            this.customerName = dto.orderStatusSaleIndexDTO.customerName;
            this.mobile = dto.orderStatusSaleIndexDTO.mobile;
            this.addressId = dto.orderStatusSaleIndexDTO.addressId;
            this.addressProvince = dto.orderStatusSaleIndexDTO.addressProvince;//省
            this.addressCity = dto.orderStatusSaleIndexDTO.addressCity;//市
            this.addressDistrict = dto.orderStatusSaleIndexDTO.addressDistrict;//区
            this.addressDetail = dto.orderStatusSaleIndexDTO.addressDetail;//详细地址
            this.zipCode = dto.orderStatusSaleIndexDTO.zipCode;//邮编
            this.materialNameCn = dto.orderStatusSaleIndexDTO.materialNameCn;//面料描述名称
            this.materialCode = dto.orderStatusSaleIndexDTO.materialCode;  //面料号
            this.composition = dto.orderStatusSaleIndexDTO.composition;//面料成分
            this.styleSizeCd = dto.orderStatusSaleIndexDTO.styleSizeCd;//尺码
            this.styleNameCn = dto.orderStatusSaleIndexDTO.styleNameCn;//款式名称
            this.refStyleId = dto.orderStatusSaleIndexDTO.refStyleId;  //  标准款id
            this.orderNo = dto.orderStatusSaleIndexDTO.orderNo; //  订单号
            this.quantity = dto.orderStatusSaleIndexDTO.quantity;    //  订单数量
            this.orderDate = dto.orderStatusSaleIndexDTO.orderDate;//订单时间
            this.orderStatus = dto.orderStatusSaleIndexDTO.orderStatus;
            this.shopId = dto.orderStatusSaleIndexDTO.shopId;
            this.shopName = dto.orderStatusSaleIndexDTO.shopName;
            this.urgent = dto.orderStatusSaleIndexDTO.urgent; //  是否加急
            this.toBeConfirm = dto.orderStatusSaleIndexDTO.toBeConfirm;  //  待确认
            this.garmentModify = dto.orderStatusSaleIndexDTO.garmentModify;//  成衣修改
            this.garmentBarcode = dto.orderStatusSaleIndexDTO.garmentBarcode;//  成衣条码
            this.price = dto.orderStatusSaleIndexDTO.price;//单价
            this.totalPrice = dto.orderStatusSaleIndexDTO.totalPrice;//总价
        }
    }

    /// <summary>
    /// 用于确认预览界面
    /// </summary>
    public class 销售日记账预览VO
    {
        //  页面显示内容
        public String orderNo { get; set; } //  订单号
        public DateTime orderDate { get; set; }     //订单时间
        public String styleNameCn { get; set; }//款式名称
        public String materialCode { get; set; }  //面料号
        public Double quantity { get; set; }    //  订单数量
        public String styleSizeCd { get; set; }//尺码
        public String customerName { get; set; } //  客户名
        public String orderStatus { get; set; } //  订单状态
        //  确认提交内容
        public String orderId { get; set; } //  订单id
        public Boolean urgent { get; set; } //  是否加急
        public Boolean toBeConfirm { get; set; }    //  待确认
        public Boolean garmentModify { get; set; }  //  成衣修改
        public String garmentBarcode { get; set; } //  成衣条码
        public 销售日记账预览VO(KashiyamaOrderStatusSaleIndexFacade页面VO dto)
        {
            this.orderNo = dto.orderNo; //  订单号
            this.orderDate = dto.orderDate;//订单时间
            this.styleNameCn = dto.styleNameCn;     //  款式
            this.materialCode = dto.materialCode;     //  面料号
            this.styleSizeCd = dto.styleSizeCd;     //  尺码
            this.quantity = dto.quantity;    //  订单数量
            this.customerName = dto.customerName;
            this.orderStatus = dto.orderStatus; //  订单状态
            //  确认提交内容
            this.orderId = dto.orderId;
            this.urgent = dto.urgent;
            this.toBeConfirm = dto.toBeConfirm;
            this.garmentModify = dto.garmentModify;
            this.garmentBarcode = dto.garmentBarcode;
        }
        public 销售日记账预览VO(OrderStatusSaleIndexDTO dto)
        {
            this.orderNo = dto.orderNo; //  订单号
            this.orderDate = dto.orderDate;//订单时间
            this.styleNameCn = dto.styleNameCn;     //  款式
            this.materialCode = dto.materialCode;     //  面料号
            this.styleSizeCd = dto.styleSizeCd;     //  尺码
            this.quantity = dto.quantity;    //  订单数量
            this.customerName = dto.customerName;
            this.orderStatus = dto.orderStatus; //  订单状态
            //  确认提交内容
            this.orderId = dto.orderId;
            this.urgent = dto.urgent;
            this.toBeConfirm = dto.toBeConfirm;
            this.garmentModify = dto.garmentModify;
            this.garmentBarcode = dto.garmentBarcode;
        }
    }
}
